package com.escope.casemanager.service.impl;

import com.escope.casemanager.dao.CaseExampleDao;
import com.escope.casemanager.dao.CaseShareDao;
import com.escope.casemanager.model.*;
import com.escope.casemanager.service.CaseExampleService;
import com.escope.casemanager.service.CaseShareService;
import com.escope.casemanager.uti.DateUtil;
import com.escope.casemanager.uti.JpaUtil;
import com.escope.casemanager.uti.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * Administrator
 * 2024/3/15
 */
@Transactional(rollbackFor = Exception.class)
@Service("caseShareService")
public class CaseShareServiceImpl implements CaseShareService {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CaseShareDao caseShareDao;
    @Autowired
    private CaseExampleDao caseExampleDao;

    @Value("${escope.share.url.prefix}")
    private String prefix;


    @Override
    //@Transactional
    public HttpResult saveOrUpdate(CaseShare share) {
        SysUserCacheInfo cacheInfo = redisUtil.getCacheUser();
        CaseShare db_share = null;
        if(share.getId() == null){
            db_share = new CaseShare();
            CaseExample example = caseExampleDao.findByIdAndBdelete(share.getExamId(), "0");
            String token = UUID.randomUUID().toString();
            db_share.setShareUrl(prefix + token);
            db_share.setDirName(example.getDirName());
            db_share.setIndex("/index.html");
            db_share.setToken(token);
            db_share.setCreateTime(new Date());
            db_share.setCreateUser(cacheInfo.getUser().getId());
            if(share.getExpireDate() == null){
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH, 7); // 未设置到期日期 默认七天
                share.setExpireDate(calendar.getTime());
            }
        }else{
            db_share = caseShareDao.findById(share.getId()).get();
            db_share.setUpdateTime(new Date());
            db_share.setUpdateUser(cacheInfo.getUser().getId());
        }
        JpaUtil.copyNotNullProperties(share, db_share);
        caseShareDao.save(db_share);
        redisUtil.setShareCacheInfo(db_share);
        return HttpResult.ok();
    }

    @Override
    public HttpResult findByExamId(Long  examId) {
        return HttpResult.ok(caseShareDao.findByExamIdAndBdelete(examId, "0"));
    }

    @Override
    public CaseShare findByToken(String token) {
        return caseShareDao.findByToken(token);
    }

    @Override
    public HttpResult delete(Long id) {
        CaseShare share = caseShareDao.findById(id).get();
        share.setBdelete("1");
        redisUtil.clearShareCacheInfo(share.getToken());
        caseShareDao.save(share);
        return HttpResult.ok();
    }

}
